<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.global.js"></script>
</head>

<body>
    <div id="app">
        <form @submit.prevent="onSubmit">
            <input type="text" v-model="userInput">
            <button>提交</button>
        </form>
        <ul>
            <todo-item v-for="todo in todos" :key="todo.id" :todo="todo" @remove="onRemove(todo.id)"></todo-item>
        </ul>
    </div>

    <script>
        const root = {
            data() {
                return {
                    userInput: "please ",
                    todos: [{
                            id: 1,
                            title: 'Do the dishes'
                        },
                        {
                            id: 2,
                            title: 'Take out the trash'
                        },
                        {
                            id: 3,
                            title: 'Mow the lawn'
                        }
                    ],
                    currentId: 4
                }
            },
            methods: {
                creatTodoId() {
                    return Date.now()
                },
                onRemove(id) {
                    this.todos = this.todos.filter(
                        todo => todo.id !== id
                    )
                },
                onSubmit() {
                    this.todos.push({
                        id:this.creatTodoId(),
                        title:this.userInput
                    })
                    this.userInput=""
                }
            }
        }

        const app = Vue.createApp(root)

        app.component(
            "todo-item", {
                template: `   
            <li>
               <h3 style="display:inline-block"> {{todo.title}}</h3>
               <button @click="$emit('remove')">狗带</button>
            </li>
            `,
                //接受来自父组件的数据 父传子 ： props down
                props: ["todo"],
                //向父组件发送的自定义事件 子传父  events up
                emits: ["remove"]
            }
        )
        // Vue.createApp(app).mount("#app")
        app.mount("#app")
    </script>
</body>

</html>